﻿using UnityEngine;
using System.Collections;
using ProgressBar;

public class CVRImage : MonoBehaviour {

	public int 			m_nID;
	public UITexture 	m_Image;
	public UITexture	m_BackImage;
	public UITexture	m_WaitingBar;
	bool				m_bLoaded;
	WWW					www;

	// Use this for initialization
	void Start () {
		m_bLoaded = false;
	}
	
	// Update is called once per frame
	void Update () {
		if(www != null)
			m_WaitingBar.GetComponent<ProgressRadialBehaviour> ().SetFillerSize (www.progress);	
		if (m_bLoaded)
			m_WaitingBar.gameObject.SetActive(false);
	}

	void OnClick()
	{
		CVRScene.Instance.ResetImages (m_nID);
	}

	public IEnumerator SetTexture()
	{
		VR_AR.VR_INFO vrInfo = JsonDataRead.Instance.vrData [JsonDataRead.Instance.curVRID];
		Texture tex = JsonDataRead.Instance.FindTextureByUrl (vrInfo.vrImages[m_nID].introImageFilePath);

		if (tex == null) {
			www = new WWW (JsonDataRead.Instance.DataPath + vrInfo.vrImages[m_nID].introImageFilePath);
			yield return www;
			m_Image.mainTexture = www.texture;
			JsonDataRead.Instance.AddTexture(vrInfo.vrImages[m_nID].introImageFilePath, www.texture);
			m_bLoaded = true;
		} else {
			m_Image.mainTexture = tex;
			m_bLoaded = true;
		}
	}
}